﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using TracedReality;

namespace Interface.UI
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            nudWidth.Value = Settings.width;
            nudHeight.Value = Settings.height;
            nudBounces.Value = Settings.numBounces;
            nudShadows.Value = Settings.numShadowSamples;
            nudAntialias.Value = Settings.numAntialiasSamples;
            pAmbient.BackColor = (System.Drawing.Color)Settings.ambientColor;
            pBack.BackColor = (System.Drawing.Color)Settings.backColor;

            cbParallel.Checked = Settings.parallelRender;
            nudTiles.Value = Settings.chunks;

            rbNone.Checked = !Settings.useSDSCaster;
            rbKDTree.Checked = !rbNone.Checked && Settings.SDSType == "KD";
            rbOctree.Checked = !rbNone.Checked && Settings.SDSType == "Oct";
            nudSDSDepth.Value = Settings.maxSDSDepth;
            nudSDSCapacity.Value = Settings.maxSDSNodeCount;

            cbPhoton.Checked = Settings.usePhotonMapping;
            nudPhotons.Value = Settings.numPhotons;
            nudPBounces.Value = Settings.numPhotonBounces;
            nudCollect.Value = Settings.numPhotonsToCollect;
            nudK.Value = (decimal)Settings.coneFilterK;
        }

        private void cbParallel_CheckedChanged(object sender, EventArgs e)
        {
            nudTiles.Enabled = cbParallel.Checked;
        }

        private void cbPhoton_CheckedChanged(object sender, EventArgs e)
        {
            nudPhotons.Enabled = nudPBounces.Enabled = nudCollect.Enabled = nudK.Enabled = cbPhoton.Checked;
        }

        private void SDSType_CheckedChanged(object sender, EventArgs e)
        {
            bool on = !rbNone.Checked;
            nudSDSDepth.Enabled = nudSDSCapacity.Enabled = on;
        }

        private void pAmbient_MouseClick(object sender, MouseEventArgs e)
        {
            cdColorPicker.Color = pAmbient.BackColor;
            if (cdColorPicker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pAmbient.BackColor = cdColorPicker.Color;
            }
        }

        private void pBack_MouseClick(object sender, MouseEventArgs e)
        {
            cdColorPicker.Color = pBack.BackColor;
            if (cdColorPicker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pBack.BackColor = cdColorPicker.Color;
            }
        }

        private void bAccept_Click(object sender, EventArgs e)
        {
            //Settings settings = new Settings();

            Settings.width = (int)nudWidth.Value;
            Settings.height = (int)nudHeight.Value;
            Settings.numBounces = (int)nudBounces.Value;
            Settings.numShadowSamples = (int)nudShadows.Value;
            Settings.numAntialiasSamples = (int)nudAntialias.Value;
            Settings.ambientColor = Vector.parseColor(pAmbient.BackColor);
            Settings.backColor = Vector.parseColor(pBack.BackColor);

            Settings.parallelRender = cbParallel.Checked;
            Settings.chunks = (int)nudTiles.Value;

            Settings.useSDSCaster = !rbNone.Checked;
            if (rbKDTree.Checked) { Settings.SDSType = "KD"; }
            else if (rbOctree.Checked) { Settings.SDSType = "Oct"; }
            Settings.maxSDSDepth = (int)nudSDSDepth.Value;
            Settings.maxSDSNodeCount = (int)nudSDSCapacity.Value;

            Settings.usePhotonMapping = cbPhoton.Checked;
            Settings.numPhotons = (int)nudPhotons.Value;
            Settings.numPhotonBounces = (int)nudPBounces.Value;
            Settings.numPhotonsToCollect = (int)nudCollect.Value;
            Settings.coneFilterK = (double)nudK.Value;

            //Settings.setGlobalSettings(ref settings);
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void bCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}
